package cn.edu.ctbu.labtest.webapi;


import cn.edu.ctbu.labtest.core.PageReq;
import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.service.StudentService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;


    /**
     * 读取的url:/webapi/student/list
     *
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll() {

        List<Student> students = studentService.findAll();


        return students;

    }

    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size,
                                   @RequestParam(value = "name") String  name) {

        Sort sort = new Sort(Sort.Direction.DESC, "id");

        Pageable pageable = new PageRequest(page, size, sort);

        Student student=new Student();

       student.setName(name);

       Page<Student> studentPage=studentService.findAll(student,pageable);

         PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalPages());



         return pageUtils;



    }



    @GetMapping("/getbypage2")
    public PageUtils getByPage2(PageReq<Student> req) {

        Sort sort = new Sort(Sort.Direction.DESC, "id");

        Pageable pageable = PageRequest.of(req.getPageIndex(),
                req.getPageSize()
                , sort);

        Student student=req.getWhereObj();
        if(student==null)
            student=new Student();


        Page<Student> studentPage=studentService.findAll(student,pageable);

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());



        return pageUtils;



    }


    /**
     * 读取的url:/webapi/student/get/id
     *
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Long id) {

        Student student = studentService.getById(id);

        student.setPassword("");


        return student;

    }


    /**
     * 新增学生的方法,/webapi/student/insert
     *
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student) {


        Student student1 = studentService.insert(student);

        return student1;


    }


    /**
     * 更新学生的方法,/webapi/student/update
     *
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student) {

        //读取旧的数据
        Student oldstudent = studentService.getById(student.getId());


        if (StringUtils.isEmpty(student.getPassword())) {
            //没有新密码，那么就是旧密码
            student.setPassword(oldstudent.getPassword());
        }

        student = studentService.update(student);

        return student;


    }

    /**
     * 删除学生的方法,/webapi/student/delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {


        studentService.delete(id);


    }


}
